package com.icim.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.icim.entity.Ret_cus_prd_indt;
import com.icim.entity.Ret_cus_so;

@Repository
public class RetCusSoDao {
    @Resource
    private JdbcTemplate icimJdbcTemplate;

    public void save(Ret_cus_so retCusSo) {
        // List<Object[]> batch = new ArrayList<Object[]>();
        //
        // for (TestTable testTable : testTableList) {
        // // 注意顺序
        // batch.add(new Object[] { testTable.getName(), testTable.getAge() });
        // }
        StringBuffer sqlBuffer = new StringBuffer("INSERT INTO RET_CUS_SO(");
        sqlBuffer.append("OPE_FAB_ID,CUS_SO_ID,CUS_SO_CATE,CUS_SO_STAT,MDL_ID_FK,");
        sqlBuffer.append("MTRL_PROD_ID_FK,PLN_PRD_QTY,CUS_SO_GRADE,PLN_STB_TIMESTAMP,CUS_PO_RLS_TIMESTAMP,");
        sqlBuffer.append("CRT_USR,CRT_TIMESTAMP,PLN_CMP_TIMESTAMP,CUS_SO_NOTE,CUS_SO_REMARK,");
        sqlBuffer.append("CUS_ID_FK");
        sqlBuffer.append(") VALUES(");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?,?,?,?,?,");
        sqlBuffer.append("?");
        sqlBuffer.append(")");

        icimJdbcTemplate.update(
                sqlBuffer.toString(),
                new Object[] { retCusSo.getOpeFabId(), retCusSo.getCusSoId(), retCusSo.getCusSoCate(),
                        retCusSo.getCusSoStat(), retCusSo.getMdlIdFk(), retCusSo.getMtrlProdIdFk(),
                        retCusSo.getPlnPrdQty(), retCusSo.getCusSoGrade(), retCusSo.getPlnStbTimestamp(),
                        retCusSo.getCusPoRlsTimestamp(), retCusSo.getCrtUsr(), retCusSo.getCrtTimestamp(),
                        retCusSo.getPlnCmpTimestamp(), retCusSo.getCusSoNote(), retCusSo.getCusSoRemark(),
                        retCusSo.getCusIdFk() });
    };
    public List<Ret_cus_so> findRetCusSoList(Collection<String> soIds){
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("SELECT WO_ID,CUS_SO_ID,CUS_SO_CATE FROM RET_WO A ,RET_CUS_SO B WHERE ");
        sqlBuffer.append("WO_TYP ='S' AND A.SO_ID = B.CUS_SO_ID ");
        sqlBuffer.append(" AND CUS_SO_ID IN (");
        sqlBuffer.append(" SELECT SO_ID FROM RET_WO WHERE WO_TYP='S' AND WO_ID IN (");
        for (String soId : soIds) {
            sqlBuffer.append("'").append(soId).append("',");
        }
        sqlBuffer.deleteCharAt(sqlBuffer.length()-1);
        sqlBuffer.append("))");
        
        return icimJdbcTemplate.query(sqlBuffer.toString(), new RowMapper<Ret_cus_so>() {

            @Override
            public Ret_cus_so mapRow(ResultSet rs, int rowNum) throws SQLException {
                Ret_cus_so retCusSo = new Ret_cus_so();
                retCusSo.setCusSoId(rs.getString("CUS_SO_ID"));
                retCusSo.setCusSoCate(rs.getString("CUS_SO_CATE"));
                retCusSo.setIcimSoId(rs.getString("WO_ID"));
                return retCusSo;
            }
        });
    }
}
